package com.lkk.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.aspectj.lang.annotation.Before;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lkk.data.ExcelDataEntity;
import com.lkk.data.ExcelDataException;
import com.lkk.data.Log;
import com.lkk.interaction.RestfulClient;
import com.lkk.util.CommonUtil;
import com.lkk.util.Constant;
import com.lkk.util.EntityListUtil;
import com.lkk.util.Red_Excel;
import com.lkk.util.CommonUtil;
/**
 * 用例类基类
 * @author admin
 *
 */
public class CaseSuper {
		//excel对象集合
		public static List<ExcelDataEntity> exDataEntityList;
		//excel对象
		public ExcelDataEntity exDataEntity;
		//用例编号
		public List<String> caseNumList = new ArrayList<String>();
		//用例名称
		public String caseName;
		//协议
		public String protocol;
		//请求方法
		public String method;
		//路径
		public String url;
		//是否加密
		public String ifEncrypt;
		//是否需要cookie
		public String ifCookie;
		//是否需要token
		public String ifToken;
		//消息头集合
		public Map<String,String> headers;
		//主体对象
		public JSONObject body;
		//断言集合
		public Map<String,String> assertions;
		//响应码
		public String status;
		//响应头集合
		public Map<String,String> responseheaders;
		//响应码
		public int responseCode;
		//响应主体
		public JSONObject responseBody;
		
		/**
		 * 获取excel数据
		 */
		@BeforeSuite
		public void getExcelData() {
			//读取excel对象
			Red_Excel re = new Red_Excel();
			//获取测试实体类list
			exDataEntityList = re.redExcel(Constant.FILEPATH);
			Log.info("--------获得excel数据成功--------");
		}
		
		/**
		 * 根据用例编号获取用例实体对象
		 */
		public void getExcelDataEntity(String caseNum) {
			for(ExcelDataEntity ex:exDataEntityList) {
				if(caseNum.equals(ex.getCaseNum())) {
					//指定对应用例序号的excel实体对象
					exDataEntity = ex;
					//给父类的用例编号赋值
					//caseNumf = caseNum;
				}
			}
		}
		
		/**
		 * 获取请求的参数
		 */
		public void getParam() {
			EntityListUtil entityListUtil =  new EntityListUtil();
			//获取访问协议
			protocol = exDataEntity.getProtocol();
			//获取请求方法
			method = exDataEntity.getMethod();
			//拼接获取访问路径
			url = protocol+"://"+exDataEntity.getUrl();
			//获取接口是否加密
			ifEncrypt = exDataEntity.getIfEncrypt();
			//获取接口是否需要cookie
			ifCookie = exDataEntity.getIfCookie();
			//获取接口是否需要token
			ifToken = exDataEntity.getIfToken();
			//获取消息头集合
			headers = entityListUtil.getHeader(exDataEntity);
			//判断body是否需要token
			if(Constant.YES.equals(ifToken.toUpperCase())) {
				//获取请求主体
				body = entityListUtil.parserBody(exDataEntity,Constant.TOKENRESULT);
			}else {
				body = entityListUtil.parserBody(exDataEntity);
			}
			
			//获取断言字段
			assertions = entityListUtil.getAssertions(exDataEntity);
			//获取断言响应码
			status = exDataEntity.getStatus();
		}
		
		/**
		 * 发送请求
		 * @throws ExcelDataException 
		 * @throws IOException 
		 * @throws ClientProtocolException 
		 */
		public void getResponse(String caseNum) {
			RestfulClient restfulClient = new RestfulClient();
			try {
				if(Constant.POST.equals(method.toUpperCase())) {
					//发送POST请求
					restfulClient.sendRequest(method, ifEncrypt, headers, body, url);
					//获取响应头
					responseheaders = 
							restfulClient.getResponseHeader();
					//获取响应码
					responseCode = restfulClient.getResponseCode();
					//获取响应主体
					responseBody = restfulClient.getResponseBody(ifEncrypt);
					
				}else if(Constant.GET.equals(method.toUpperCase())){
					//发送GET请求
					restfulClient.sendRequest(method, url);
					//获取响应头
					responseheaders = restfulClient.getResponseHeader();
					//获取响应码
					responseCode = restfulClient.getResponseCode();
					//获取响应主体
					responseBody = restfulClient.getResponseBody(ifEncrypt);
				}
			} catch (ParseException e) {
				Log.error(e.getMessage());
			} catch (ExcelDataException e) {
				Log.error(e.getMessage());
			} catch (IOException e) {
				Log.error(e.getMessage());
			}
			//判断用例编号是否是获取token的用例
			if(Constant.TOKENCASENUM.equals(caseNum)) {
				//从响应主体中获取token
				CommonUtil.getToken(responseBody);
			}
			
		}

}
